package cn.com.skbang.backend.user.dao.impl;

import cn.com.skbang.backend.user.dao.impl.mapper.entity.Teacher;
import cn.com.skbang.backend.user.dao.impl.mapper.entity.TeacherExample;
import cn.com.skbang.backend.common.core.base.Page;
import cn.com.skbang.backend.common.core.base.PageUtil;
import cn.com.skbang.backend.common.core.tools.IdWorker;
import cn.com.skbang.backend.user.dao.TeacherDao;
import cn.com.skbang.backend.user.dao.impl.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TeacherDaoImpl implements TeacherDao {
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public int save(Teacher record) {
        if (record.getId() == null) {
            record.setId(IdWorker.getId());
        }
        return this.teacherMapper.insertSelective(record);
    }

    @Override
    public int deleteById(Long id) {
        return this.teacherMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateById(Teacher record) {
        record.setGmtCreate(null);
        record.setGmtModified(null);
        return this.teacherMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Teacher getById(Long id) {
        return this.teacherMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<Teacher> page(int pageCurrent, int pageSize, TeacherExample example) {
        int count = this.teacherMapper.countByExample(example);
        pageSize = PageUtil.checkPageSize(pageSize);
        pageCurrent = PageUtil.checkPageCurrent(count, pageSize, pageCurrent);
        int totalPage = PageUtil.countTotalPage(count, pageSize);
        example.setLimitStart(PageUtil.countOffset(pageCurrent, pageSize));
        example.setPageSize(pageSize);
        return new Page<Teacher>(count, totalPage, pageCurrent, pageSize, this.teacherMapper.selectByExampleWithBLOBs(example));
    }

    @Override
    public List<Teacher> listByStatusId(Integer statusId) {
        TeacherExample example = new TeacherExample();
        TeacherExample.Criteria criteria = example.createCriteria();
        criteria.andStatusIdEqualTo(statusId);
        return this.teacherMapper.selectByExample(example);
    }

    @Override
    public List<Teacher> getByLecturerName(String LecturerName) {
        TeacherExample example = new TeacherExample();
        TeacherExample.Criteria criteria = example.createCriteria();
        criteria.andLecturerNameLike(PageUtil.rightLike(LecturerName));
        List<Teacher> resultList = this.teacherMapper.selectByExample(example);
        if (resultList.isEmpty()) {
            return null;
        }
        return resultList;
    }

    @Override
    public List<Teacher> listByIds(List<Long> ids) {
        TeacherExample example = new TeacherExample();
        TeacherExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        return this.teacherMapper.selectByExample(example);
    }
}
